﻿using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DICE_GS;

public class MyInfo {

	public static readonly MyInfo Instance = new MyInfo();

	public int DBID = 0;
	public string NickName = "?Unknown";
	
    public int userlevel = 0;
    public int userLevel
    {
        get { return userlevel; }
        set { userlevel = value; TopInfoBar.Instance.UpdateInfos(); }
    }

    int money = 0;
    public int Money
    {
        get { return money; }
        set { money = value; TopInfoBar.Instance.UpdateInfos(); }
    }

	public int diamond = 0;
    public int Diamond
    {
        get { return diamond; }
        set { diamond = value; TopInfoBar.Instance.UpdateInfos(); }
    }

    public int HPPotion = 0;
    //public int HPPotion
    //{
    //    get { return hpPotion; }
    //    set { hpPotion = value; TopInfoBar.Instance.UpdateInfos(); }
    //}

	public int ResurrectPotion = 0;
	
    public int stamina = 0;
    public int Stamina
    {
        get { return stamina; }
        set { stamina = value; TopInfoBar.Instance.UpdateInfos(); }
    }

	public int MaxStamina = 0;
	public DateTime NextTickTimeForStamina = new DateTime();
	public double NextTickSecondForStamina = 0f;
    public int AdditionalInvenCount = 0;
	public Int64 Exp = 0;
	public int Score = 0;
    public string LastLoginTime = "";
	public int leaderClassId = 0;
	public int pawnId = 0;
	public int gachaPoint = 0;
    public int gachaTicket = 0;

	public int maxUnitCost 
	{
		get
		{
			CPlayerLevelBonusData bonusData = CPlayerLevelBonusDataManager.Instance.GetPlayerLevelBonusData(userLevel);
			if( bonusData != null )
				return bonusData.max_cost;
			else
				return 0;
		}
	}

    public int DailyStampId = 0;    // 출석팝업 종류
    public int DailyStampCount = -1; // 연속 출석 일수

    public int MissionCount;
    public int[] DailyMissionIDList;

    public Dictionary<int, DeckInfo> MyDeckMap = new Dictionary<int, DeckInfo>();

    public List<UnitInfo> MyUnitsList = new List<UnitInfo>();

    public List<FriendInfo> MyFriendList = new List<FriendInfo>();
    public List<FriendInfo> MyFriendRequestedList = new List<FriendInfo>();
    public List<MailInfo> MyMailboxList = new List<MailInfo>();

	public List<int> MySkillList = new List<int>();
	public List<int> MyCharacterList = new List<int>();
	
	public int LastSeletedUnitSlot = -1;

	public bool isLevelUp = false;
	public int nextUserLevel = 0;

	public List<AchievementInfo> AchievementList = new List<AchievementInfo>();
    public List<AchievementValue> AchievementValueList = new List<AchievementValue>();

    public PVEStageInfo MyPVEStageInfo = new PVEStageInfo();

	public int[] ChanceSkill = new int[2];
	public DeckInfo currentDeckInfo
	{
		get
		{
			return MyDeckMap[MyCurrentDeck];
		}
	}

	public int currentInvenCount
	{
		get
		{
			return CGSettingDataManager.Instance.GetInventoryExtendCount()+AdditionalInvenCount;
		}
	}

	public MyInfo()
	{
		
	}

	private int _MyCurrendDeck = -1;
	public int MyCurrentDeck
	{
		get
		{
			if( _MyCurrendDeck == -1 )
			{
				for(int i=0; i<MyDeckMap.Count; i++)
				{
					if( MyDeckMap[i].order == 1 )
					{
                        Debug.LogError(MyDeckMap[i].deckId);
						_MyCurrendDeck = i;
						break;
					}
				}
			}
			return _MyCurrendDeck;
		}
		set
		{
			_MyCurrendDeck = value;
		}
	}
	public int MyCurrentDeckDBID = 0;
	public int LastSelectedUnitID = 0;
	public int LastSelectedUnitDBID = 0;
	public ArrayList LastSelectedUnitIDs = new ArrayList();
	public ArrayList LastSelectedUnitDBIDs = new ArrayList();
	public void SetMyUnitDeckImg()
	{
		if (MyDeckMap.ContainsKey(MyCurrentDeck))
		{
			DeckInfo tmpDeck = MyDeckMap[MyCurrentDeck];
			if (tmpDeck != null)
			{
				foreach (KeyValuePair<int, UnitInfo> pair in tmpDeck.SlotMap)
				{
					GameObject go = null;
					switch (pair.Key)
					{
						case 0:
							{
								go = GameObject.Find("LeaderImg");
							}
							break;
						case 1:
							{
								go = GameObject.Find("Sub1Img");
							}
							break;
						case 2:
							{
								go = GameObject.Find("Sub2Img");
							}
							break;
						case 3:
							{
								go = GameObject.Find("Sub3Img");
							}
							break;
						case 4:
							{
								go = GameObject.Find("Sub4Img");
							}
							break;
						default:
							{
								glog.debug("Unknown slot index");
							}
							break;
					}

                    if (pair.Value != null)
                    {
                        foreach (UnitInfo info in MyUnitsList)
                        {
                            if (info.UnitDBID == pair.Value.UnitDBID)
                            {
                                pair.Value.UnitClassID = info.UnitClassID;
                                break;
                            }
                        }
                        CUnitData tmpUnitData = CUnitDataManager.Instance.GetUnitData(pair.Value.UnitClassID);

                        if (tmpUnitData != null)
                        {
                            if (go != null)
                            {
                                UISprite tmpSprite = go.GetComponent<UISprite>() as UISprite;
                                tmpSprite.spriteName = tmpUnitData.name;
                            }
                        }
                        else
                        {
                            if (go != null)
                            {
                                UISprite tmpSprite = go.GetComponent<UISprite>() as UISprite;
                                if (pair.Key == 0)
                                {
                                    tmpSprite.spriteName = "leader_back";
                                }
                                else
                                {
                                    tmpSprite.spriteName = "sub_back";
                                }
                            }
                        }
                    }
                    else
                    {
                        Debug.LogError("null");
                    }
				}
			}
		}		
	}
	
	public UnitInfo GetUnitInfoById(int UnitDBId)
	{
		foreach(UnitInfo info in MyUnitsList)
		{
			if( info.UnitDBID == UnitDBId )
				return info;
		}
		
		return null;
	}
	
	public void AddStamina(int addValue)
	{
		Debug.Log("MyInfo.AddStamina");
		Stamina += addValue;
		
		if( Stamina > MaxStamina )
			Stamina = MaxStamina;
	}

	public bool IsExistUnitInDeckMap(int unitDBId)
	{
		for (int k = 0; k < MyInfo.Instance.MyDeckMap.Count; k++)
		{
			for (int j = 0; j < MyInfo.Instance.MyDeckMap[k].SlotMap.Count; j++)
			{
				if (MyInfo.Instance.MyDeckMap[k].SlotMap[j].UnitDBID == unitDBId)
				{
					return true;
				}
			}
		}

		return false;
	}
}

public class DeckInfo
{
	public Dictionary<int, UnitInfo> SlotMap = new Dictionary<int, UnitInfo>();
	public string DeckName = "덱";
	public int deckId = 0;
	public int order = 0;
	public int skill1 = 0;
	public int skill2 = 0;
	
	public DeckInfo()
	{
		
	}
	
	public DeckInfo(DeckInfo deckInfo)
	{
		this.SlotMap = new Dictionary<int, UnitInfo>(deckInfo.SlotMap);
		this.DeckName = deckInfo.DeckName;
		this.deckId = deckInfo.deckId;
		this.order = deckInfo.order;
		this.skill1 = deckInfo.skill1;
		this.skill2 = deckInfo.skill2;
	}

	public void Clear()
	{
		SlotMap.Clear();
	}

	public void AddUnitInfo(int slot, UnitInfo info)
	{
		SlotMap[slot] = info;
	}

    public CSkillData skill1Data
    {
        get
        {
            CSkillData data = CSkillDataManager.Instance.FindSkillData(skill1);
            if (data != null)
                return data;
            else
                return null;
        }
    }

    public CSkillData skill2Data
    {
        get
        {
            CSkillData data = CSkillDataManager.Instance.FindSkillData(skill2);
            if (data != null)
                return data;
            else
                return null;
        }
    }

	
	public string skill1_IconName
	{
		get
		{
			CSkillData data = CSkillDataManager.Instance.FindSkillData(skill1);
			if( data != null )
			{
				return data.icon_name;
			}
			return null;
		}
	}
	
	public string skill2_IconName
	{
		get
		{
			CSkillData data = CSkillDataManager.Instance.FindSkillData(skill2);
			if( data != null )
			{
				return data.icon_name;
			}
			return null;
		}
	}
	
	public int currentActivateCardCount
	{
		get
		{
			int count = 0;
			foreach(KeyValuePair<int, UnitInfo> pair in SlotMap)
			{
				if( pair.Value.UnitDBID != 0 )
					count++;
			}
			return count;
		}
	}

	public int GetSameCardCount(int unitClassId)
	{
		int count = 0;
		foreach(KeyValuePair<int, UnitInfo> pair in SlotMap)
		{
			if( pair.Value.UnitClassID == unitClassId )
			{
				count++;
			}
		}
		
		return count;
	}
}

public class UnitInfo
{
    public bool isChecked = false;
	public int UnitDBID = 0;
	public int UnitClassID = 0;
	public int UnitLevel = 1;
	public int exp = 0;

    static public void AllUnitInfosUnCheck(List<UnitInfo> list)  // 씬 초기화 및 씬 나갈때 체크 해제
    {
        foreach (UnitInfo temp in list)
        {
            temp.isChecked = false;
        }
    }
    
	public UnitInfo(int DBID, int classID, int level=1, int exp=0, int grade=1)
	{
		this.UnitDBID = DBID;
		this.UnitClassID = classID;
		this.UnitLevel = level;
		this.exp = exp;
	}

    public CUnitData unitData
    {
        get
        {
			CUnitData unitData = CUnitDataManager.Instance.GetUnitData(UnitClassID);
			if( unitData != null )
            	return CUnitDataManager.Instance.GetUnitData(UnitClassID);
			else
				return null;
        }
    }

    public int hp
    {
        get
        {
            CUnitData tmpUnitData = CUnitDataManager.Instance.GetUnitData(UnitClassID);
			if( tmpUnitData != null )
			{
				CUnitAbilityItem plusAbility_LV1 = CUnitAbilityDataManager.Instance.GetUnitAbilityData("1_" + tmpUnitData.unit_class);
            	CUnitAbilityItem plusAbility = CUnitAbilityDataManager.Instance.GetUnitAbilityData(UnitLevel + "_" + tmpUnitData.unit_class);
				if( UnitLevel > 1 && plusAbility != null &&  plusAbility_LV1 != null )
		            return tmpUnitData.max_hp + plusAbility_LV1.max_hp + plusAbility.max_hp;
				else if( UnitLevel == 1 && plusAbility != null )
					return tmpUnitData.max_hp + plusAbility.max_hp;
				else
					return tmpUnitData.max_hp;
			}
			else
				return 0;
        }
    }

    public int attack
    {
        get
        {
            CUnitData tmpUnitData = CUnitDataManager.Instance.GetUnitData(UnitClassID);
			if( tmpUnitData != null )
			{
				CUnitAbilityItem plusAbility_LV1 = CUnitAbilityDataManager.Instance.GetUnitAbilityData("1_" + tmpUnitData.unit_class);
            	CUnitAbilityItem plusAbility = CUnitAbilityDataManager.Instance.GetUnitAbilityData(UnitLevel + "_" + tmpUnitData.unit_class);
				if( plusAbility != null && plusAbility_LV1 != null )
            		return tmpUnitData.attack_base + plusAbility_LV1.attack + plusAbility.attack;
				else
					return tmpUnitData.attack_base;
			}
			else
				return 0;
        }
    }

    public int defense
    {
        get
        {
	        CUnitData tmpUnitData = CUnitDataManager.Instance.GetUnitData(UnitClassID);
			if( tmpUnitData != null )
			{
				CUnitAbilityItem plusAbility_LV1 = CUnitAbilityDataManager.Instance.GetUnitAbilityData("1_" + tmpUnitData.unit_class);
	            CUnitAbilityItem plusAbility = CUnitAbilityDataManager.Instance.GetUnitAbilityData(UnitLevel + "_" + tmpUnitData.unit_class);
				if( plusAbility != null && plusAbility_LV1 != null )
					return tmpUnitData.defense_base + plusAbility_LV1.defense + plusAbility.defense;
				return tmpUnitData.defense_base;
			}
			else
				return 0;
        }
    }

    public int move
    {
        get
        {
            CUnitData tmpUnitData = CUnitDataManager.Instance.GetUnitData(UnitClassID);
			if( tmpUnitData != null )
			{
				CUnitAbilityItem plusAbility_LV1 = CUnitAbilityDataManager.Instance.GetUnitAbilityData("1_" + tmpUnitData.unit_class);
            	CUnitAbilityItem plusAbility = CUnitAbilityDataManager.Instance.GetUnitAbilityData(UnitLevel + "_" + tmpUnitData.unit_class);
				if( plusAbility != null && plusAbility_LV1 != null )
            		return tmpUnitData.move_base + plusAbility_LV1.move +  plusAbility.move;
				else
					return tmpUnitData.move_base;
			}
			else
				return 0;
        }
    }

	public int grade
	{
		get
		{
			CUnitData tmpUnitData = CUnitDataManager.Instance.GetUnitData(UnitClassID);
			if( tmpUnitData != null )
				return tmpUnitData.grade;
			else
				return 0;
		}
	}

    public int cost
    {
        get
        {
            CUnitData tmpUnitData = CUnitDataManager.Instance.GetUnitData(UnitClassID);
			if( tmpUnitData != null )
            	return tmpUnitData.unit_cost;
			else
				return 0;
        }
    }

    public element element
    {
        get
        {
            CUnitData tmpUnitData = CUnitDataManager.Instance.GetUnitData(UnitClassID);
			if( tmpUnitData != null )
            	return tmpUnitData.element;                
			else
				return element.none;
        }
    }
	
	public string name
	{
		get
		{
			CUnitData tmpUnitData = CUnitDataManager.Instance.GetUnitData(UnitClassID);
			if( tmpUnitData != null )
				return CStringTableManager.Instance.GetItemByName(tmpUnitData.name_string);
			else
				return "";
		}
	}
	
	public string spriteName
	{
		get
		{
			CUnitData tmpUnitData = CUnitDataManager.Instance.GetUnitData(UnitClassID);
			if( tmpUnitData != null )
				return tmpUnitData.name;
			else 
				return "";
		}
	}
}

public class AchievementInfo
{
	public int achievementId;
	public int achievementLevel;
	public DateTime updateTime;
    public bool isReward;
}

public class AchievementValue
{
    public int achievementLogname;
    public int achievementData;   
}

public class PVEStageInfo
{
    int p = 1;

    public int StoryMode
    {
        get
        {
            return p;
        }

        set
        {
            Debug.LogError("StoryMode --- " + value);
            p = value;
        }
    }
    public List<int> EventMode = new List<int>();

    public void Clear()
    {
        StoryMode = 1;
        EventMode.Clear();
    }
}